import matplotlib.pyplot as plt
import pygame
import sys
from utils.func import draw_image, draw_text, experience_needed
from all_class.pet_class import Pet
from all_class.block_class import Block
from all_class.player_class import Player
from all_class.box_class import Button, Tooltip
from tool_class.status_judging import status_judging
from tool_class import Buy
from tool_class import level_judging


# 初始化 Pygame
pygame.init()

# 设置屏幕尺寸
screen_width = 768
screen_height = 768
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置窗口标题
pygame.display.set_caption('V-pet')

# 设置帧率
clock = pygame.time.Clock()
fps = 60

# 游戏状态
running = True

# 导入图片1
img_background1 = pygame.image.load('./v-pet./static./img./background1.png')
img_pet_normal = pygame.image.load('./v-pet./static./img./pet.png')
img_pet = img_pet_normal

# 创建按钮对象1
choose_1 = Button(50, 50, 150, 50, '继续游玩', 30, (255, 200, 0), (255, 0, 0))
choose_2 = Button(50, 100, 150, 50, '新建存档', 30, (200, 30, 150), (255, 0, 0))

# 选择游玩方式
while running :
    for event in pygame.event.get() :
        if event.type == pygame.QUIT :
            running = False
        elif event.type == pygame.KEYDOWN :
            if event.key == pygame.K_ESCAPE :
                running = False
    # 绘制背景            
    draw_image(screen, img_background1, 768, 768, (0, 0))
    
    # 绘制按钮
    choose_1.draw(screen)
    choose_2.draw(screen)
    
    #绘制图片
    draw_image(screen, img_pet, 190, 184, (300, 300))
    
    # 按钮操作判断
    choose_1.check_hover(pygame.mouse.get_pos())
    if choose_1.is_clicked(pygame.mouse.get_pos(), pygame.mouse.get_pressed()) == True :
        
        # 加载宠物对象
        pet_1 = Pet((300, 510), (190, 184), '李唐王丁甄')
        pet_1.load_data("./v-pet./data./pet_data.csv")
        
        # 加载玩家对象
        player_1 = Player('知识学爆', 100, 200)
        player_1.load_data("v-pet./data./player_data.csv")
        
        break
        
    choose_2.check_hover(pygame.mouse.get_pos())
    if choose_2.is_clicked(pygame.mouse.get_pos(), pygame.mouse.get_pressed()) == True :
        
        # 创建宠物对象
        pet_1 = Pet((300, 510), (190, 184), '李唐王丁甄')
        
        # 创建玩家对象
        player_1 = Player('知识学爆', 100, 200)
        
        break
    
    # 更新屏幕
    pygame.display.flip()

    # 控制帧率
    clock.tick(fps)

# 导入图片
img_background2 = pygame.image.load('./v-pet./static./img./background2.png')
img_pet_enjoy = pygame.image.load('./v-pet./static./img./pet_enjoy.png')
img_pet_cute = pygame.image.load('./v-pet./static./img./pet_cute.png')
img_pet_dirty = pygame.image.load('./v-pet./static./img./pet_dirty.png')
img_pet_grievance = pygame.image.load('./v-pet./static./img./pet_grievance.png')
img_pet_sick = pygame.image.load('./v-pet./static./img./pet_sick.png')
img_pet_sad = pygame.image.load('./v-pet./static./img./sad.png')
img_mahuateng = pygame.image.load('./v-pet./static./img./mahuateng.png')
img_mayun = pygame.image.load('./v-pet./static./img./mayun.png')

# 创建方块对象2
background_white = Block(screen, '背景', (200, 200, 200), (15, 65), (245, 200), 100, 100, 10)
hunger_block = Block(screen, '饥饿', (255, 255, 0), (50, 100), (200, 20), 100, 100, 3)
mood_block = Block(screen, '心情', (0, 255, 0), (50, 130), (200, 20), 100, 100, 3)
clean_block = Block(screen, '干净', (0, 0, 255), (50, 160), (200, 20), 100, 100, 3)
health_block = Block(screen, '健康', (255, 0, 0), (50, 190), (200, 20), 100, 100, 3)
exp_block = Block(screen, '经验', (255, 255, 255), (50, 220), (200, 20),100, 300, 3)

# 创建按钮对象2
pay_feeding = Button(700, 444, 60, 60, '喂食', 50,(255, 0, 0), (0, 0, 0))
pay_cleaning = Button(700, 534, 60, 60, '清洁', 50, (70, 70, 200), (0, 0, 0))
pay_treatment = Button(700, 624, 60, 60, '治疗', 50, (0, 255, 0), (0, 0, 0))
play = Button(300, 510, 190, 184, '玩耍', 50, (0, 255, 0), (0, 0, 0))
remake = Button(630, 680, 90, 60, '重开', 50, (0, 255, 0), (0, 0, 0))
recharge = Button(600, 135, 90, 60, '充值', 50, (0, 255, 0), (0, 0, 0))

#创建提示框对象
tooltip_pay_feeding = Tooltip('喂食  5金币', None, (0, 0, 0), (0, 0, 0))
tooltip_play = Tooltip('玩耍  10体力', None, (0, 0, 0), (0, 0, 0))
tooltip_pay_cleaning = Tooltip( '清洁  10金币', None, (0, 0, 0), (0, 0, 0))
tooltip_pay_treatment = Tooltip('治疗  15金币', None, (0, 0, 0), (0, 0, 0))
tooltip_recharge = Tooltip('充值  ????', None, (0, 0, 0), (0, 0, 0))

#创建购买行为对象
buy_feeding = Buy(5, 10, 50, img_mahuateng, '购买成功', screen)
buy_cleaning = Buy(10, 10, 50, img_mahuateng, '购买成功', screen)
buy_treatment = Buy(15, 10, 50, img_mahuateng, '购买成功', screen)
buy_play = Buy(10, 10, 50, img_mahuateng, '宠物很高兴！', screen)

#创建等级经验列表
level_ = level_judging(10)

index = 0
loop = 0

# 游戏主循环
while running:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    # 更新游戏状态
    if pet_1.health <= 0 :
        draw_image(screen, img_mahuateng, 768, 768, (0, 0))
        draw_text(screen, '游戏结束', (270, 100), None, (255, 255, 255), size = 60)
        remake.draw(screen)
        remake.check_hover(pygame.mouse.get_pos())
        if remake.is_clicked(pygame.mouse.get_pos(), pygame.mouse.get_pressed()) == True :
            pet_1 = Pet((300, 510), (190, 184), '李唐王丁甄')
            
        pygame.display.flip()
        clock.tick(fps)
        continue
    
    # 这里可以添加你的游戏逻辑
    
    # 根据状态更新宠物图片
    status = status_judging(pet_1.hunger, pet_1.mood, pet_1.clean, pet_1.health)
    if status.count(2) == 1 and pet_1.hunger < 30 :
        img_pet = img_pet_sad
    elif status.count(2) == 1 and pet_1.mood < 30 :
        img_pet = img_pet_sad
    elif status.count(2) == 1 and pet_1.clean < 30 :
        img_pet = img_pet_dirty
    elif status.count(2) == 1 and pet_1.health < 30 :
        img_pet = img_pet_sick
    elif status.count(2) >= 2 :
        img_pet = img_pet_grievance
    elif status.count(3) == 4 : 
        img_pet = img_pet_enjoy
    else :
        img_pet = img_pet_normal

    # 绘制背景
    #screen.fill((0, 0, 0))  # 填充黑色背景
    draw_image(screen, img_background2, 768, 768, (0, 0))#填充图片背景
    
    # 绘制状态条
    background_white.draw_block_2(screen)
    hunger_block.draw_block_1(screen)
    mood_block.draw_block_1(screen)
    clean_block.draw_block_1(screen)
    health_block.draw_block_1(screen)
    exp_block.draw_block_1(screen)
    
    # 绘制按钮
    pay_feeding.draw(screen)
    pay_cleaning.draw(screen)
    pay_treatment.draw(screen)
    recharge.draw(screen)
    
    # 绘制文本
    draw_text(screen, pet_1.pet_name, (50, 75), size = 20)
    draw_text(screen, 'level'+str(pet_1.level), (190, 75), size = 20)
    draw_text(screen, hunger_block.name, (20, 100), size = 15)
    draw_text(screen, mood_block.name, (20, 130), size = 15)
    draw_text(screen, clean_block.name, (20, 160), size = 15)
    draw_text(screen, health_block.name, (20, 190),size = 15)
    draw_text(screen, exp_block.name, (20, 220), size = 15)
    draw_text(screen, player_1.name, (600, 15), size = 40)
    draw_text(screen, '金币: '+str(player_1.money), (600, 75), size = 20)
    draw_text(screen, '体力: '+str(player_1.physical), (600, 105), size = 20)

    # 绘制游戏对象
    draw_image(screen, img_pet, pet_1.breath_width, pet_1.breath_height, (pet_1.breath_x, pet_1.breath_y))
    
    # 操作判断
    
    #判断购买
    pay_feeding.check_hover(pygame.mouse.get_pos())
    if pay_feeding.is_hovered == True :
        tooltip_pay_feeding.draw(screen)
    if pay_feeding.is_clicked(pygame.mouse.get_pos(), pygame.mouse.get_pressed()) == True :
        player_1.money, pet_1.hunger , pet_1.now_exp, ending = buy_feeding.pay_return(player_1.money, pet_1.hunger, pet_1.now_exp)
    buy_feeding.ending_draw(pygame.mouse.get_pos())

    #判断购买
    pay_cleaning.check_hover(pygame.mouse.get_pos())
    if pay_cleaning.is_hovered == True :
        tooltip_pay_cleaning.draw(screen)
    if pay_cleaning.is_clicked(pygame.mouse.get_pos(), pygame.mouse.get_pressed()) == True :
        player_1.money, pet_1.clean, pet_1.now_exp, ending = buy_cleaning.pay_return(player_1.money, pet_1.clean, pet_1.now_exp)
    buy_cleaning.ending_draw(pygame.mouse.get_pos())
            
    #        
    pay_treatment.check_hover(pygame.mouse.get_pos())
    if pay_treatment.is_hovered == True :
        tooltip_pay_treatment.draw(screen)
    if pay_treatment.is_clicked(pygame.mouse.get_pos(), pygame.mouse.get_pressed()) == True :
        player_1.money, pet_1.health, pet_1.now_exp, ending = buy_treatment.pay_return(player_1.money, pet_1.health, pet_1.now_exp)
    buy_treatment.ending_draw(pygame.mouse.get_pos())
    
    #        
    play.check_hover(pygame.mouse.get_pos())
    if play.is_hovered == True :
        tooltip_play.draw(screen)
    if play.is_clicked(pygame.mouse.get_pos(), pygame.mouse.get_pressed()) == True :
        player_1.physical, pet_1.mood, pet_1.now_exp, ending = buy_play.pay_return(player_1.physical, pet_1.mood, pet_1.now_exp)
    buy_play.ending_draw(pygame.mouse.get_pos())
    
    #判断充值按钮
    recharge.check_hover(pygame.mouse.get_pos())
    if recharge.is_hovered == True :
        tooltip_recharge.draw(screen)
    if recharge.is_clicked(pygame.mouse.get_pos(), pygame.mouse.get_pressed()) == True :
        player_1.money += 40
        loop = 100
        
    if loop > 0 :
        draw_image(screen, img_mayun, 240, 150, (550, 175))
        loop -= 1
    
    
    # 宠物呼吸动态控制
    index+=1
    index%=9999999
    pet_1.breath_(index)
    pet_1.breath()
    
    # 数据变化
    hunger_block.data_progress(pet_1.hunger)
    mood_block.data_progress(pet_1.mood)
    clean_block.data_progress(pet_1.clean)
    health_block.data_progress(pet_1.health)
    exp_block.data_progress(pet_1.now_exp)
    if pet_1.health > 0 and pet_1.health <= 100 and pet_1.hunger <=0 :
        pet_1.health -= 0.001
    if pet_1.mood > 0 and pet_1.mood <= 100:
        pet_1.mood -= 0.01
    if pet_1.clean > 0 and pet_1.clean <= 100:
        pet_1.clean -= 0.01
    if pet_1.hunger > 0 and pet_1.hunger <= 100:
        pet_1.hunger -= 0.01
    
    #等级判断
    pet_1.level, pet_1.now_exp = level_.get_level(pet_1.level, pet_1.now_exp)
    exp_block.data_end = level_.level_exp[str(pet_1.level)]
    
    # 更新屏幕
    pygame.display.flip()

    # 控制帧率
    clock.tick(fps)

if index > 0 :
    #保存玩家数据
    player_1.save_data()

    #保存宠物数据
    pet_1.save_data()

# 退出 Pygame
pygame.quit()
sys.exit()